package com.tengju.bff.interfaces.shared;

import com.tengju.user.domain.model.sign.SignStatisticsDetail;
import com.tengju.user.domain.shared.CastUtil;
import com.tengju.user.domain.shared.DateUtil;

import java.io.BufferedWriter;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.math.BigDecimal;
import java.nio.charset.StandardCharsets;
import java.util.*;

/**
 * @author chenhang
 * @Date 2021/1/11
 */
public class SignAssembler {
    public static byte[] starListToByte(List<SignStatisticsDetail> signStatisticsDetailList) {
        List<LinkedHashMap<String, Object>> exportData = new ArrayList<>();
        LinkedHashMap<String, Object> header = new LinkedHashMap<>();
        header.put("会员编号","会员编号");
        header.put("签约星级","签约星级");
        header.put("签约进度","签约进度");
        header.put("姓名","姓名");
        header.put("昵称","昵称");
        header.put("上级编号","上级编号");
        header.put("上级姓名","上级姓名");
        header.put("上级昵称","上级昵称");
        header.put("SEC编号","SEC编号");
        header.put("SEC姓名","SEC姓名");
        header.put("SEC昵称","SEC昵称");
        header.put("EC编号","EC编号");
        header.put("EC姓名","EC姓名");
        header.put("EC昵称","EC昵称");
        exportData.add(header);
        for (SignStatisticsDetail signStatisticsDetail : signStatisticsDetailList) {
            LinkedHashMap<String, Object> row = new LinkedHashMap();
            row.put("idCode",signStatisticsDetail.getIdCode().getValue());
            row.put("level",signStatisticsDetail.getStarLevel().getDesc());
            String stepDesc = "";
            switch (signStatisticsDetail.getSignStep()) {
                case SEND_ALREADY:
                case SURVEY_ALREADY:
                case ACCOUNT_ALREADY:
                case AUDIT_ALREADY:
                case EXAM_ALREADY:
                    stepDesc = "未签约";
                    break;
                case SIGN_ALREADY:
                case END:
                    stepDesc = "已签约";
                    break;
            }
            row.put("step",stepDesc);
            row.put("getName",signStatisticsDetail.getName());
            row.put("getNickname",signStatisticsDetail.getNickname());
            row.put("getUpperCode",signStatisticsDetail.getUpperCode());
            row.put("getUpperName",signStatisticsDetail.getUpperName());
            row.put("getUpperNickname",signStatisticsDetail.getUpperNickname());
            row.put("getSecId",signStatisticsDetail.getSecId());
            row.put("getSecName",signStatisticsDetail.getSecName());
            row.put("getSecNickname",signStatisticsDetail.getSecNickname());
            row.put("getEcId",signStatisticsDetail.getEcId());
            row.put("getEcName",signStatisticsDetail.getEcName());
            row.put("getEcNickname",signStatisticsDetail.getEcNickname());
            exportData.add(row);
        }
        return exportCSV(new ByteArrayOutputStream(),exportData);
    }


    public static byte[] statisticsDetailsToByte(List<SignStatisticsDetail> signStatisticsDetailList) {
        List<LinkedHashMap<String, Object>> exportData = new ArrayList<>();
        LinkedHashMap<String, Object> header = new LinkedHashMap<>();
        header.put("会员编号","会员编号");
        header.put("签约星级","签约星级");
        header.put("签约进度","签约进度");
        header.put("认证身份","认证身份");
        header.put("上个月出货额","上个月出货额");
        header.put("本月差额","本月差额");
        header.put("邀约时间","邀约时间");
        header.put("手机号码","手机号码");
        exportData.add(header);
        for (SignStatisticsDetail signStatisticsDetail : signStatisticsDetailList) {
            LinkedHashMap<String, Object> row = new LinkedHashMap();
            row.put("idCode",signStatisticsDetail.getIdCode().getValue());
            row.put("level",signStatisticsDetail.getStarLevel().getDesc());
            String stepDesc = "";
            switch (signStatisticsDetail.getSignStep()) {
                case SEND_ALREADY:
                    stepDesc = "问卷未填写";
                    break;
                case SURVEY_ALREADY:
                    stepDesc = "待认证";
                    break;
                case ACCOUNT_ALREADY:
                    stepDesc = "待审批";
                    break;
                case AUDIT_ALREADY:
                    stepDesc = "待考试";
                    break;
                case EXAM_ALREADY:
                    stepDesc = "待签合同";
                    break;
                case SIGN_ALREADY:
                    stepDesc = "签约完成";
                    break;
                case END:
                    stepDesc = "签约完成";
                    break;
            }
            row.put("step",stepDesc);
            row.put("accountType",signStatisticsDetail.getAccountType() == null ? "" : signStatisticsDetail.getAccountType().getDesc());
            row.put("lastMonthMoneyOut",
                    signStatisticsDetail.getLastMonthMoneyOut().compareTo(BigDecimal.valueOf(50000)) >= 0 ?
                            "5W+" : "");
            row.put("thisMonthGap",signStatisticsDetail.getThisMonthMoneyOut().compareTo(BigDecimal.valueOf(50000)) >= 0 ?
                    "已达标" : 50000 - signStatisticsDetail.getThisMonthMoneyOut().intValue());
            row.put("inviteTime", DateUtil.formartLocalDateTime("yyyy-MM-dd",signStatisticsDetail.getInviteTime()));
            row.put("mobile",signStatisticsDetail.getMobile());
            exportData.add(row);
        }
        return exportCSV(new ByteArrayOutputStream(),exportData);
    }

    public static byte[] exportCSV(ByteArrayOutputStream out,List<LinkedHashMap<String, Object>> exportData) {
        BufferedWriter buffCvsWriter = null;
        try {
            buffCvsWriter = new BufferedWriter(new OutputStreamWriter(out, StandardCharsets.UTF_8));
            // 将body数据写入表格
            for (Iterator<LinkedHashMap<String, Object>> iterator = exportData.iterator(); iterator.hasNext(); ) {
                fillDataToCsv(buffCvsWriter, iterator.next());
                if (iterator.hasNext()) {
                    buffCvsWriter.newLine();
                }
            }
            // 刷新缓冲
            buffCvsWriter.flush();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            // 释放资源
            if (buffCvsWriter != null) {
                try {
                    buffCvsWriter.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return out.toByteArray();
    }

    private static void fillDataToCsv(BufferedWriter buffCvsWriter, LinkedHashMap row) throws IOException {
        Map.Entry propertyEntry;
        for (Iterator<Map.Entry> propertyIterator = row.entrySet().iterator(); propertyIterator.hasNext(); ) {
            propertyEntry = propertyIterator.next();
            buffCvsWriter.write("\"" + CastUtil.castString(propertyEntry.getValue()) + "\"");
            if (propertyIterator.hasNext()) {
                buffCvsWriter.write(",");
            }
        }
    }
}
